<template>
  <div class="page-container">
    <!--工具栏-->
    <div class="toolbar">
      <el-form :inline="true" :model="filters" :size="size">
        <el-row class="search-input">
          <el-form-item label="类目树名称：">
            <el-input v-model="filters.name" clearable placeholder="类目树名称"/>
          </el-form-item>
          <el-form-item label="类型：">
            <el-select clearable v-model="filters.type" placeholder="类型">
              <el-option label="后台类目" value="1"></el-option>
              <el-option label="前台类目" value="2"></el-option>
            </el-select>
          </el-form-item>
        </el-row>
        <el-row class="search-button">
          <el-form-item>
            <kt-button
              icon="fa fa-search"
              :label="$t('action.search')"
              type="primary"
              @click="findPage(null)"
            />
          </el-form-item>
          <el-form-item>
            <kt-button
              icon="fa fa-refresh"
              :label="$t('action.reset')"
              type="primary"
              @click="resetForm('filters')"
            />
          </el-form-item>
          <el-form-item>
            <kt-button
              icon="fa fa-plus"
              :label="$t('action.add')"
              type="primary"
              @click="handleAdd"
            />
          </el-form-item>
        </el-row>
      </el-form>
    </div>
    <!--表格内容栏-->
    <cy-table
      :height="350"
      :highlightCurrentRow="true"
      :content="pageResult"
      :totalSize="totalSize"
      v-loading="lodaing"
      :columns="columns"
      :showSelection="false"
      :showBatchDelete="false"
      @findPage="findPage"
      @handleEdit="handleEdit"
      @handleDelete="handleDelete"
      ref="CyTable"
    ></cy-table>
    <!--新增界面-->
    <el-dialog
      :title="operation ? '新增' : '编辑'"
      width="40%"
      height="800px"
      :visible.sync="addDialogVisible"
      :close-on-click-modal="false"
    >
      <el-form
        :model="dataForm"
        label-width="120px"
        :rules="dataFormRules"
        ref="dataForm"
        :size="size"
        :inline="true"
      >
        <el-row class="searchDialog-input">
            <el-form-item label="ID：" prop="id" v-if="false">
          <el-input v-model="dataForm.id" :disabled="true" auto-complete="off" placeholder="请输入ID"></el-input>
        </el-form-item>
        <el-form-item label="类目树名称：" prop="name">
          <el-input v-model="dataForm.name" oninput="if(value.length>20)value=value.slice(0,20)" auto-complete="off" placeholder="请输入类目树名称"></el-input>
        </el-form-item>
        <el-form-item label="类目树编码：" prop="categoryCode">
          <el-input v-model="dataForm.categoryCode" oninput="if(value.length>60)value=value.slice(0,60)" auto-complete="off" placeholder="请输入类目树编码"></el-input>
        </el-form-item>
        <el-form-item label="第三方系统编码：" prop="thirdCode">
          <el-input v-model="dataForm.thirdCode" oninput="if(value.length>60)value=value.slice(0,60)" auto-complete="off" placeholder="请输入第三方系统编码"></el-input>
        </el-form-item>
        <el-form-item label="类目树类型：" prop="type">
          <el-select v-model="dataForm.type" placeholder="类型" style="width:200px">
            <el-option label="后台类目树" value="1" v-if="showType"></el-option>
            <el-option label="前台类目树" value="2"></el-option>
          </el-select>
        </el-form-item>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align:center;">
        <el-button :size="size" @click.native="addDialogVisible = false">{{ $t("action.cancel") }}</el-button>
        <el-button
          :size="size"
          type="primary"
          @click.native="submitForm"
          :loading="editLoading"
        >{{ $t("action.submit") }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import CyTable from "@/views/Core/ProductCategoryTable";
import KtButton from "@/views/Core/KtButton";
export default {
  components: {
    KtButton,
    CyTable,
  },
  data() {
    return {
      lodaing:true,
      showType:false,
      size: "small",
      filters: {
        name: "",
        type: ""
      },
     columns:[
        { prop: "name", label: "类目树名称", minWidth: 120 },
        { prop: "categoryCode", label: "类目编码", minWidth: 120 },
        {
          prop: "type",
          label: "类目类型",
          minWidth: 120,
          formatter: function(row, column, cellValue, index) {
            if (cellValue == "1") {
              return "后台类目树";
            } else if (cellValue == "2") {
              return "前台类目树";
            }
          }
        },
        { prop: "thirdCode", label: "第三方编码", minWidth: 120 }
      ],
      totalSize: 0,
      pageResult: [],
      pageRequest: { pageNum: 1, pageSize: 10 },
      editLoading: false,
      operation: false, // true:新增, false:编辑
      editDialogVisible: false, //编辑界面是否显示
      addDialogVisible: false, //新增界面是否显示
      dataFormRules: {
        name: [{ required: true, message: "请输入类目名称", trigger: "blur" }],
        categoryCode: [{ required: true, message: "请输入类目树编码", trigger: "blur" }]
      },
      // 新增编辑界面数据
      dataForm: {
        id: "",
        name: "",
        categoryCode: "",
        thirdCode: "",
        type: "2"
      }
    };
  },
  mounted() {
    this.findPage();
    this.isAddAfterCategory()
  },
  methods: {
    // 获取分页数据
    findPage: function(params) {
      let this_ = this;
      if (params == undefined || params == null) {
        params = {
          pageNum: this_.pageRequest.pageNum,
          pageSize: this_.pageRequest.pageSize
        };
      }
      if (this_.filters == undefined || this_.filters == null) {
        this_.filters = {};
      }
      this_.filters.start = params.pageNum;
      this_.filters.limit = params.pageSize;
      this.utils.request.queryPageProductCategory(this_.filters, function(res) {
        if (res.data.rows == null) {
          res.data.rows = [];
        }
        this_.pageResult = res.data.rows;
        this_.totalSize = Number(res.data.total);
        this_.lodaing=false
      });
    },
    // 批量删除
    handleDelete: function(data) {
      let param = {};
      param.id = data.params[0].id;
      this.utils.request.deleteProductCategory(param, data.callback);
    },
    // 显示新增界面
    handleAdd: function() {
      this.addDialogVisible = true;
      this.operation = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
      });
    },
    // 显示编辑界面
    handleEdit: function(params) {
      this.$router.push({
        path: "/ProductCategoryEdit" + "/" + params.row.id + "/" + params.row.type});
    },
    // 编辑
    submitForm: function() {
      this.$refs.dataForm.validate(valid => {
        if (valid) {
          this.$confirm("确认提交吗？", "提示", {}).then(() => {
            let params = Object.assign({}, this.dataForm);
            var this_ = this;
            this.utils.request.saveProductCategory(params, function(res) {
              if (res.code == "0000") {
                this_.$message({ message: "操作成功", type: "success" });
                this_.$refs["dataForm"].resetFields();
                this_.addDialogVisible = false;
                this_.findPage(null);
                this_.isAddAfterCategory()
              } else {
                this_.$message({
                  message: "操作失败, " + res.msg,
                  type: "error"
                });
              }
            });
          });
        }
      });
    },
    // 重置
    resetForm(formName) {
      this.filters = {
        name: "",
        shop_id: "",
        status: ""
      };
      this.findPage();
    },
    //判断当前部门是否增加后台一级类目
    isAddAfterCategory: function(data) {
      let this_=this
      let param = {};
      this.utils.request.isAddAfterCategory(param, function (res) {
        if(res.code=="0000"){
          this_.showType=res.data
        }
      });
    },
  }
};
</script>

<style scoped lang='scss'>
</style>
